package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.io.HttpUtil;
import com.ecoolex.ying.space.core.dao.UserWorksOpusDAO;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksOpus;
import com.ecoolex.ying.space.entity.UserWorksOpus.Column;
import com.ecoolex.ying.space.pojo.AddUserWorksOpusDTO;
import com.ecoolex.ying.space.pojo.AddUserWorksOpusItemsDTO;
import com.ecoolex.ying.space.pojo.EditUserWorksOpusDTO;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserWorksOpusManager {
	
    @Autowired
    private UserWorksOpusDAO dao;
    
    @Autowired
    private UserWorksOpusItemsManager userWorksOpusItemsManager;
    
    public void addUserWorksOpus(UserWorks userWorks, AddUserWorksOpusDTO addUserWorksOpusDTO, Integer userId, String createUser) {
    	UserWorksOpus userWorksOpus = new UserWorksOpus();
    	userWorksOpus.setUserId(userWorks.getUserId());
    	userWorksOpus.setWorksId(userWorks.getWorksId());
    	if(Check.notNull(addUserWorksOpusDTO.getVrLink())) {
    		BizAssert.isTrue(HttpUtil.isHttpUrl(addUserWorksOpusDTO.getVrLink()), ResultCode.VERIFY.build(10), "链接无效");
    		userWorksOpus.setVrLink(addUserWorksOpusDTO.getVrLink());
    	}
    	userWorksOpus.setStatementDescription(addUserWorksOpusDTO.getStatementDescription());
    	userWorksOpus.setAreaDescription(addUserWorksOpusDTO.getAreaDescription());
    	userWorksOpus.setStyleDescription(addUserWorksOpusDTO.getStyleDescription());
    	userWorksOpus.setDeleted(UserWorksOpus.NOT_DELETED);
    	userWorksOpus.setCreateUser(createUser);
    	userWorksOpus.setCreateTime(Date.from(Instant.now()));
    	userWorksOpus.setUpdateUser(createUser);
    	userWorksOpus.setUpdateTime(Date.from(Instant.now()));
    	
    	dao.insert(userWorksOpus);
    	//处理作品栏目
    	List<AddUserWorksOpusItemsDTO> addUserWorksOpusItemsDTOList = addUserWorksOpusDTO.getAddUserWorksOpusItemsDTOList();
    	userWorksOpusItemsManager.updateUserWorksOpusItems(userWorks, addUserWorksOpusItemsDTOList, createUser);
    }
    
    public void editUserWorksOpus(UserWorks userWorks,EditUserWorksOpusDTO editUserWorksOpusDTO, Integer userId, String updateUser) {
    	UserWorksOpus userWorksOpus = getByWorksId(editUserWorksOpusDTO.getWorksId());
    	BizAssert.isTrue(Check.notNull(userWorksOpus),ResultCode.VERIFY.build(10), "作品不存在");
    	if(Check.notNull(editUserWorksOpusDTO.getVrLink())) {
    		BizAssert.isTrue(HttpUtil.isHttpUrl(editUserWorksOpusDTO.getVrLink()), ResultCode.VERIFY.build(11), "链接无效");
    		userWorksOpus.setVrLink(editUserWorksOpusDTO.getVrLink());
    	}
    	userWorksOpus.setStatementDescription(editUserWorksOpusDTO.getStatementDescription());
    	userWorksOpus.setAreaDescription(editUserWorksOpusDTO.getAreaDescription());
    	userWorksOpus.setStyleDescription(editUserWorksOpusDTO.getStyleDescription());
    	userWorksOpus.setUserId(userId);
    	userWorksOpus.setUpdateUser(updateUser);
    	userWorksOpus.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(userWorksOpus, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    	
    	//更新栏目
    	List<AddUserWorksOpusItemsDTO> userWorksOpusItemsDTOList = editUserWorksOpusDTO.getAddUserWorksOpusItemsDTOList();
    	userWorksOpusItemsManager.updateUserWorksOpusItems(userWorks, userWorksOpusItemsDTOList, updateUser);
    }
    
    public void delete(UserWorks userWorks, String updateUser) {
    	UserWorksOpus userWorksOpus = getByWorksId(userWorks.getWorksId());
    	userWorksOpus.setDeleted(UserWorksOpus.IS_DELETED);
    	userWorksOpus.setUpdateUser(updateUser);
    	userWorksOpus.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(userWorksOpus, Column.deleted,Column.updateUser,Column.updateTime);
    	//删除栏目
    	userWorksOpusItemsManager.deleteByWorksId(userWorks, updateUser);
    }
    
    public UserWorksOpus getByWorksId(Integer worksId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(worksId, UserWorksOpus.NOT_DELETED);
    }
}